﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Serilog;
using Serilog.Sinks.Elasticsearch;
using Serilog.Sinks.Kafka;

namespace Demo.Net.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SerilogController : ControllerBase
    {

        public SerilogController()
        {
            
        }


        /// <summary>
        /// 使用示例
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> LogTest()
        {

            //using var log = new LoggerConfiguration()
            //.WriteTo.Console()
            //.CreateLogger();

           // Log.Logger = new LoggerConfiguration()
           //.MinimumLevel.Debug()
           //.WriteTo.Console()
           //.WriteTo.File("logs/myapp.txt", rollingInterval: RollingInterval.Day)  //存到 文件
           //.CreateLogger();

            Log.Information("Hello, world!");

            Log.Verbose("This is a verbose log message.");
            Log.Debug("This is a debug log message.");
            Log.Information("This is an informational log message.");
            Log.Warning("This is a warning log message.");
            Log.Error("This is an error log message.");
            Log.Fatal("This is a fatal log message.");

//     Log.CloseAndFlush();

            //int a = 10, b = 0;
            //try
            //{
            //    Log.Debug("Dividing {A} by {B}", a, b);
            //    Console.WriteLine(a / b);
            //}
            //catch (Exception ex)
            //{
            //    Log.Error(ex, "Something went wrong");
            //}
            //finally
            //{
            //    await Log.CloseAndFlushAsync();
            //}

            ////        Log.Logger = new LoggerConfiguration().WriteTo.con
            return Ok();
        }


        /// <summary>
        /// 存到ElasticSearch
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> LogToEs()
        {
            Log.Logger = new LoggerConfiguration()
            .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://192.168.31.154:9200"))
            {
               IndexFormat ="SerilogTest",
                AutoRegisterTemplate = true
            })
            .CreateLogger();

            for (int i = 0; i < 1000000; i++)
            {
                // 在应用程序中使用Log.Logger记录日志信息
                Log.Information($@"This is a log message sent to Elasticsearch{i}");
            }
            Log.CloseAndFlush();
            return Ok();
        }


        /// <summary>
        /// 写入到 Pgsql
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> LogToPgsql()
        {
            Log.Logger = new LoggerConfiguration()
             .WriteTo.PostgreSQL(
                connectionString: "Host=192.168.31.154;Port=5432;Username=postgres;Password=test123;Database=DemoNetLog;Pooling=true;Maximum Pool Size=100",
             tableName: "SeriLogTest",
                needAutoCreateTable: true)
               .CreateLogger();

            for (int i = 0; i < 1000000; i++)
            {
                Log.Information($@"This is a log message sent to PgSql{i}");
            }

            Log.CloseAndFlush();
            return Ok();
        }


        [HttpGet]
        public async Task<IActionResult> LogToKafka()
        {
            Log.Logger = new LoggerConfiguration()
          .WriteTo.Kafka(bootstrapServers:"192.168.31.154:9092",topic:"SerilogTest").CreateLogger();
           Log.Information($@"This is a log message sent to Kafka");
            Log.CloseAndFlush();
            return Ok();
        }



     
    }
}
